package org.devbg.academy.corejava.shop.ui.frames;

import javax.swing.JPanel;
import java.awt.GridBagLayout;

import javax.swing.JDialog;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JComboBox;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.List;

import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import org.devbg.academy.corejava.shop.db.daos.OrderDAO;
import org.devbg.academy.corejava.shop.db.entities.Order;
import org.devbg.academy.corejava.shop.ui.models.OrderItemTableModel;

public class OrdersDialog extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel selectOrderLabel = null;
	private JComboBox ordersComboBox = null;
	private JLabel dateLabel = null;
	private JTextField dateTextField = null;
	private JLabel orderItemsLabel = null;
	private JScrollPane tableScrollPane = null;
	private JTable orderItemTable = null;
	private OrderDAO orderDao = new OrderDAO();
	private List<Order> orders = Collections.<Order> emptyList();
	private OrderItemTableModel orderItemTableModel = null;

	public OrdersDialog() {
		super();
		initialize();
	}

	private void initialize() {
		this.setSize(400, 300);
		this.setContentPane(getJContentPane());
		this.setTitle("Поръчки");
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = GridBagConstraints.BOTH;
			gridBagConstraints4.gridy = 3;
			gridBagConstraints4.weightx = 1.0;
			gridBagConstraints4.weighty = 1.0;
			gridBagConstraints4.gridwidth = 2;
			gridBagConstraints4.insets = new Insets(0, 15, 15, 15);
			gridBagConstraints4.gridx = 0;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.gridwidth = 2;
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.insets = new Insets(0, 15, 10, 0);
			gridBagConstraints3.gridy = 2;
			orderItemsLabel = new JLabel();
			orderItemsLabel.setText("Артикули на поръчката: ");
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.fill = GridBagConstraints.BOTH;
			gridBagConstraints2.gridy = 1;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.insets = new Insets(10, 10, 10, 15);
			gridBagConstraints2.gridx = 1;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.insets = new Insets(10, 15, 10, 10);
			gridBagConstraints11.gridy = 1;
			dateLabel = new JLabel();
			dateLabel.setText("Дата на поръчката: ");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.insets = new Insets(15, 10, 10, 15);
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.insets = new Insets(15, 15, 10, 10);
			gridBagConstraints.anchor = GridBagConstraints.WEST;
			gridBagConstraints.gridy = 0;
			selectOrderLabel = new JLabel();
			selectOrderLabel.setText("Избери поръчка: ");
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jContentPane.add(selectOrderLabel, gridBagConstraints);
			jContentPane.add(getOrdersComboBox(), gridBagConstraints1);
			jContentPane.add(dateLabel, gridBagConstraints11);
			jContentPane.add(getDateTextField(), gridBagConstraints2);
			jContentPane.add(orderItemsLabel, gridBagConstraints3);
			jContentPane.add(getTableScrollPane(), gridBagConstraints4);
		}
		return jContentPane;
	}

	public void showDialog() {
		try {
			setModal(true);
			setVisible(true);
		} finally {
			dispose();
		}
	}

	private JComboBox getOrdersComboBox() {
		if (ordersComboBox == null) {
			ordersComboBox = new JComboBox(getOrdersId());
			ordersComboBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Long id = (Long) ordersComboBox.getSelectedItem();
					Order o = null;
					for (Order order : orders) {
						if (order.getId() == id.longValue()) {
							o = order;
							break;
						}
					}
					dateTextField.setText(o.getOrderDate().toString());
					getOrderItemTableModel().changeOrder(o);					
				}
			});
		}
		return ordersComboBox;
	}

	@SuppressWarnings("unchecked")
	private Long[] getOrdersId() {
		this.orders = orderDao.findAll();
		Long[] ordersId = new Long[orders.size()];
		for (int i = 0; i < orders.size(); i++) {
			ordersId[i] = orders.get(i).getId();
		}
		return ordersId;
	}

	private JTextField getDateTextField() {
		if (dateTextField == null) {
			dateTextField = new JTextField();
			dateTextField.setEditable(false);
		}
		return dateTextField;
	}

	private JScrollPane getTableScrollPane() {
		if (tableScrollPane == null) {
			tableScrollPane = new JScrollPane();
			tableScrollPane.setViewportView(getorderItemTable());
		}
		return tableScrollPane;
	}

	private JTable getorderItemTable() {
		if (orderItemTable == null) {
			orderItemTable = new JTable(getOrderItemTableModel());
		}
		return orderItemTable;
	}

	private OrderItemTableModel getOrderItemTableModel() {
		if (orderItemTableModel == null) {
			orderItemTableModel = new OrderItemTableModel();
		}
		return orderItemTableModel;
	}
	
	
}
